package com.shaoshaohuo.app.ui.ec;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.SpecificationAdapter;
import com.shaoshaohuo.app.adapter.VarietyGridviewAdapter;
import com.shaoshaohuo.app.constant.EcOption;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.FileType;
import com.shaoshaohuo.app.db.DaoUtils;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.EcCategorySecond;
import com.shaoshaohuo.app.entity.EcCategoryThird;
import com.shaoshaohuo.app.entity.EcVarietyEntity;
import com.shaoshaohuo.app.entity.EditSupplyPublished;
import com.shaoshaohuo.app.entity.PoiEntity;
import com.shaoshaohuo.app.entity.Specification;
import com.shaoshaohuo.app.entity.SupplyDetailEntity;
import com.shaoshaohuo.app.entity.SupplyOrder;
import com.shaoshaohuo.app.entity.UploadImageEntity;
import com.shaoshaohuo.app.entity.post.SupplyPublished;
import com.shaoshaohuo.app.framework.Logger;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.utils.AreaUtil;
import com.shaoshaohuo.app.utils.CameraUtil;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.shaoshaohuo.app.utils.ImageLoadUtil;
import com.shaoshaohuo.app.utils.PoiEntityUtils;
import com.shaoshaohuo.app.utils.SpecificationDataUtil;
import com.shaoshaohuo.app.utils.StringUtil;
import com.shaoshaohuo.app.utils.ViewUtil;
import com.shaoshaohuo.app.view.DatepickerPopwindow;
import com.shaoshaohuo.app.view.DatepickerPopwindow.OnSelectDateListener;
import com.shaoshaohuo.app.view.DatepickerStartPopwindow;
import com.shaoshaohuo.app.view.MeasureGridView;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.citymenu.CascadingMenuPopWindow;
import com.shaoshaohuo.app.view.citymenu.entity.Area;
import com.shaoshaohuo.app.view.citymenu.interfaces.CascadingMenuViewOnSelectListener;

import org.hybridsquad.android.library.CropHandler;
import org.hybridsquad.android.library.CropHelper;
import org.hybridsquad.android.library.CropParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发布模块 中 我要 卖 货
 *
 * @author cao
 */
public class SellsGoodsActivity extends BaseActivity implements OnClickListener, CropHandler, OnCheckedChangeListener {

    private static final int SELECT_ADDRESS_POI = 1;
    private static final int SELECT_PRODUCT_TYPE = SELECT_ADDRESS_POI + 1;
    private static final int SELECT_SPECIFICATIONS = SELECT_PRODUCT_TYPE + 1;
    private static final int UPLOAD_PHOTO1 = SELECT_SPECIFICATIONS + 1;
    private static final int UPLOAD_PHOTO2 = UPLOAD_PHOTO1 + 1;
    private static final int UPLOAD_PHOTO3 = UPLOAD_PHOTO2 + 1;
    private static final int UPLOAD_PHOTO4 = UPLOAD_PHOTO3 + 1;
    private static final int UPLOAD_PHOTO5 = UPLOAD_PHOTO4 + 1;
    private static final int UPLOAD_PHOTO6 = UPLOAD_PHOTO5 + 1;

    private TopbarView mTopbarView;
    private MeasureGridView mPinzhongGv;
    private  MeasureGridView gv_specification;
   private FrameLayout fl_specification;
    private Map<Specification, List<Specification>> selectSpecification = new HashMap<>();
    private View mProductLayout;
    private TextView mProductEdit;

    private View mStartTimeLayout;
    private TextView mStartTimeText; // 开始时间 view
    private View mEndTimeLayout;
    private TextView mEndTimeText; // 结束时间 view
    private View mRecivAddressLayout;
    private TextView mRecivAddressText; // 发货 地址
    private EditText mDetailAddressText; // 详细地址
    private EditText mHaimai; // 发布 到 商机 中的 我还 卖
    private EditText mGongsi; // 发布 到 商机中 的 曾经 供应 的 公司
    private View layout_specifications;
    private TextView mBuyUnitText;
    private View mBuyUnitLayout;
    private EditText mBuyPriceEdit; // 商品 价格
    private EditText mQidingxianzhiEdit; // 起售限制
    private TextView mLimitUnitText; // 起售限制 单位

//    private LinearLayout layout_price_type;
//    private TextView textview_price_type; //价格类型

    private ImageView mPhoto1Image;
    private ImageView mPhoto2Image;
    private ImageView mPhoto3Image;
    private ImageView mPhoto4Image;
    private ImageView mPhoto5Image;
    private ImageView mPhoto6Image;

    private CheckBox mShuaihuoCb;

    private ImageView mOpenView;
    private boolean isOpen = false;
    private TextView mPinZhongView;
    private boolean isAnim = false;
    private int height;
    private CheckBox mReleaseTo; // 是否 发布到商机
    private LinearLayout mReleaseView;

    private Button mNextButton;

    private AlertDialog priceDialog;
    private AlertDialog unitDialog;
    private DatepickerStartPopwindow startDatePop;
    private DatepickerPopwindow endDatePop;
    private int currentSelectImageType;
    CropParams mCropParams = new CropParams();
    private CascadingMenuPopWindow cascadingMenuPopWindow;
    private VarietyGridviewAdapter<EcCategoryThird> mVarietyAdapter;

    private String sendDate = ""; // 开始时间
    private String wantDate = ""; // 结束时间

    protected String unit; // 商品 价格单位
    private PoiEntity startPoi;
    private EcCategorySecond ecCategorySecond; // 商品 名称
    private ArrayList<EcCategoryThird> mSelectVarietyList = new ArrayList<>();

    private String mPhoto1Uri;
    private String mPhoto2Uri;
    private String mPhoto3Uri;
    private String mPhoto4Uri;
    private String mPhoto5Uri;
    private String mPhoto6Uri;

    private ScrollView scrollView;

    private String images;
    protected EcCategoryThird selectVariety; // 商品品种

    private static final int DECIMAL_DIGITS = 1;
    private int releaseHight;
    private String editRecordId;

    private boolean isEditStatus = false;
    public static final String EXTRA_RECORD_ID = "EXTRA_RECORD_ID";
    private SupplyOrder currentSupplyOrder;
    private RadioGroup priceTypeGroup;
    private RadioButton rbChangdi;
    private RadioButton rbShichange;
    private String priceType = "origin";//默认产地价格

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new TopbarView(this));
        editRecordId = getIntent().getStringExtra(EXTRA_RECORD_ID);
        if (editRecordId != null) {
            isEditStatus = true;
        }
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                setContentView(R.layout.activity_sells_goods);
                initView();
                setUpView();
                if (isEditStatus) {
                    initEditData();
                }
            }
        }, 200);
    }

    /**
     * 初始化编辑状态下的数据
     */
    private void initEditData() {
        if (isEditStatus) {
            mNextButton.setText("确认修改");
        }
        startLoadingDialog();
        RequestService.getInstance().getSupplyDetail(this, editRecordId, SupplyDetailEntity.class, new RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    SupplyDetailEntity entity = (SupplyDetailEntity) resultData;
                    updateEditUi(entity);
                } else {
                    showToast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    private void updateEditUi(SupplyDetailEntity entity) {
        SupplyOrder so = entity.getData();
        currentSupplyOrder = so;
        String[] images = so.getImages();
        initImageViews(images);

        mProductEdit.setText(so.getTitle());
        initCategory(so.getBigpid(), so.getPid(), so.getCatid());
        // 填充数据
        selectSpecification = SpecificationDataUtil.list2map(entity.getData().specifications);

        if (!selectSpecification.isEmpty()) {
            gv_specification.setAdapter(new SpecificationAdapter(this, gv_specification, selectSpecification));

            fl_specification.setVisibility(View.VISIBLE);
            layout_specifications.setVisibility(View.GONE);
        }else{
            fl_specification.setVisibility(View.GONE);
            layout_specifications.setVisibility(View.VISIBLE);
        }

        if ("origin".equals(so.getMoneyType())) {
            rbChangdi.setChecked(true);
        } else if ("market".equals(so.getMoneyType())) {
            rbShichange.setChecked(true);
        }

        mBuyPriceEdit.setText(so.getMoney());
        mBuyUnitText.setText(so.getUnit());
        mLimitUnitText.setText(so.getUnit());
        if ("1".equals(so.getShuai())) {
            mShuaihuoCb.setChecked(true);
        } else {
            mShuaihuoCb.setChecked(false);
        }
        mQidingxianzhiEdit.setText(so.getMinsell());
        String addressName = AreaUtil.getAreaFullname(so.getCityid());
        startPoi = PoiEntityUtils.convert(DaoUtils.findArea(so.getCityid()), addressName);
        mRecivAddressText.setText(addressName);
        mDetailAddressText.setText(so.getAddress());
        if (!StringUtil.isEmpty(so.getStart())) {
            mStartTimeText.setText(so.getStart());
        }
        if (!StringUtil.isEmpty(so.getEnd())) {
            mEndTimeText.setText(so.getEnd());
        }
        if (!StringUtil.isEmpty(so.getContent())) {
            edittext_mark.setText(so.getContent());
        }
    }

    private void initCategory(String bigPid, String pid, final String catid) {
        ecCategorySecond = DaoUtils.findEcCategorySecond(pid);
        RequestService.getInstance().getEcCategoryVariety(this, bigPid, pid,
                EcVarietyEntity.class, new RequestListener() {

                    @Override
                    public void onSuccess(int requestCode, BaseEntity resultData) {
                        if (resultData.isOk()) {
                            EcVarietyEntity entity = (EcVarietyEntity) resultData;
                            ArrayList<EcCategoryThird> mSelectVarietyList = (ArrayList<EcCategoryThird>) entity.getData().getList();
                            for (EcCategoryThird ec : mSelectVarietyList) {
                                if (ec.getCatid() != null && ec.getCatid().equals(catid)) {
                                    selectVariety = ec;
                                    mPinZhongView.setText(selectVariety.getCatname());
                                    mPinZhongView.setVisibility(View.VISIBLE);
                                }
                            }
                        }
                    }

                    @Override
                    public void onFailed(int requestCode, Exception error, String msg) {

                    }
                });
    }

    private void initImageViews(String[] images) {
        if (images != null) {
            for (int i = 0; i < images.length; i++) {
                String im = images[i];
                String fieldName = "mPhoto" + (i + 1) + "Image";
                String fieldUriName = "mPhoto" + (i + 1) + "Uri";
                try {
                    ImageLoader.getInstance().displayImage(im,
                            (ImageView) getClass().getDeclaredField(fieldName).get(this), ImageLoadUtil.build);
                    getClass().getDeclaredField(fieldUriName).set(this, im);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

        mReleaseTo = (CheckBox) findViewById(R.id.ib_release_to);
        hideRelease();
        scrollView = (ScrollView) findViewById(R.id.scrollview);
        mPinZhongView = (TextView) findViewById(R.id.tv_pinzhong);


        mPinzhongGv = (MeasureGridView) this.findViewById(R.id.gv_pinzhong);
        fl_specification = (FrameLayout) findViewById(R.id.fl_specification);
        gv_specification = (MeasureGridView) findViewById(R.id.gv_specification);
        mOpenView = (ImageView) findViewById(R.id.iv_open);
        //备注
        edittext_mark = (EditText) findViewById(R.id.edittext_mark);

        mShuaihuoCb = (CheckBox) this.findViewById(R.id.cb_shuaihuo);

        mProductLayout = this.findViewById(R.id.layout_product);
        mProductEdit = (TextView) this.findViewById(R.id.edittext_product);
        layout_specifications = this.findViewById(R.id.layout_specifications);
        mStartTimeLayout = this.findViewById(R.id.layout_start_time);
        mStartTimeText = (TextView) this.findViewById(R.id.textview_start_time);
        mEndTimeLayout = this.findViewById(R.id.layout_end_time);
        mEndTimeText = (TextView) this.findViewById(R.id.textview_end_time);
        mRecivAddressLayout = this.findViewById(R.id.layout_recev_address);
        mRecivAddressText = (TextView) this.findViewById(R.id.textview_recev_address);
        mDetailAddressText = (EditText) this.findViewById(R.id.textview_recev_address_detail);
        mHaimai = (EditText) this.findViewById(R.id.textview_contact_name);
        mGongsi = (EditText) this.findViewById(R.id.textview_contact_phone);

//        layout_price_type = (LinearLayout) findViewById(R.id.layout_price_type);
//        textview_price_type = (TextView) findViewById(R.id.textview_price_type);
        priceTypeGroup = (RadioGroup) findViewById(R.id.layout_price_type);
        rbChangdi = (RadioButton) findViewById(R.id.price_chandi);
        rbShichange = (RadioButton) findViewById(R.id.price_shichang);

        mBuyPriceEdit = (EditText) this.findViewById(R.id.edittext_buy_price);
        mBuyUnitText = (TextView) this.findViewById(R.id.textview_buy_unit);
        mBuyUnitLayout = this.findViewById(R.id.layout_buy_unit);
        mQidingxianzhiEdit = (EditText) this.findViewById(R.id.edittext_qishouxianzhi);
        mLimitUnitText = (TextView) this.findViewById(R.id.textview_qishouxianzhi_unit);

        mPhoto1Image = (ImageView) this.findViewById(R.id.imageview_photo1);
        mPhoto2Image = (ImageView) this.findViewById(R.id.imageview_photo2);
        mPhoto3Image = (ImageView) this.findViewById(R.id.imageview_photo3);
        mPhoto4Image = (ImageView) this.findViewById(R.id.imageview_photo4);
        mPhoto5Image = (ImageView) this.findViewById(R.id.imageview_photo5);
        mPhoto6Image = (ImageView) this.findViewById(R.id.imageview_photo6);

        mNextButton = (Button) this.findViewById(R.id.button_next);
    }

    private void setUpView() {
        mTopbarView.setCenterText("我要卖货");
        mTopbarView.setLeftView(true, true);

        mReleaseTo.setOnCheckedChangeListener(this);
        mOpenView.setOnClickListener(this);

        mPhoto1Image.setOnClickListener(this);
        mPhoto2Image.setOnClickListener(this);
        mPhoto3Image.setOnClickListener(this);
        mPhoto4Image.setOnClickListener(this);
        mPhoto5Image.setOnClickListener(this);
        mPhoto6Image.setOnClickListener(this);
        mProductLayout.setOnClickListener(this);
        mBuyUnitLayout.setOnClickListener(this);
        mStartTimeLayout.setOnClickListener(this);
        mEndTimeLayout.setOnClickListener(this);
        mRecivAddressLayout.setOnClickListener(this);
        mNextButton.setOnClickListener(this);
        layout_specifications.setOnClickListener(this);

        fl_specification.setVisibility(View.GONE);
        gv_specification.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //选择产品
                if (selectVariety == null) {
                    showToast("请先选择品种");
                    return;
                } else {
                    selectSpecifications();
                }
            }
        });

        priceTypeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int radioButtonId = group.getCheckedRadioButtonId();

                if (radioButtonId == rbChangdi.getId()) {
                    priceType = "origin";
                }else {
                    priceType = "market";
                }
            }
        });

        mPinzhongGv.setOnItemClickListener(mVarietyItemListener);

        InputFilter lengthfilter = new InputFilter() {
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                // 删除等特殊字符，直接返回
                if ("".equals(source.toString())) {
                    return null;
                }
                String dValue = dest.toString();
                String[] splitArray = dValue.split("//.");
                if (splitArray.length > 1) {
                    String dotValue = splitArray[1];
                    int diff = dotValue.length() + 1 - DECIMAL_DIGITS;
                    if (diff > 0) {
                        return source.subSequence(start, end - diff);
                    }
                }
                return null;
            }
        };

        // mBuyPriceEdit.setText("0.00");
        mBuyPriceEdit.addTextChangedListener(new TextWatcher() {
            private boolean isChanged = false;

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);
                        mBuyPriceEdit.setText(s);
                        mBuyPriceEdit.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    mBuyPriceEdit.setText(s);
                    mBuyPriceEdit.setSelection(2);
                }

                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        mBuyPriceEdit.setText(s.subSequence(0, 1));
                        mBuyPriceEdit.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                /*
				 * // TODO Auto-generated method stub if (isChanged) {//
				 * ----->如果字符未改变则返回 return; } String str = s.toString();
				 * 
				 * isChanged = true; String cuttedStr = str; 删除字符串中的dot for (int
				 * i = str.length() - 1; i >= 0; i--) { char c = str.charAt(i);
				 * if ('.' == c) { cuttedStr = str.substring(0, i) +
				 * str.substring(i + 1); break; } } 删除前面多余的0 int NUM =
				 * cuttedStr.length(); int zeroIndex = -1; for (int i = 0; i <
				 * NUM - 2; i++) { char c = cuttedStr.charAt(i); if (c != '0') {
				 * zeroIndex = i; break; }else if(i == NUM - 3){ zeroIndex = i;
				 * break; } } if(zeroIndex != -1){ cuttedStr =
				 * cuttedStr.substring(zeroIndex); } 不足3位补0 if
				 * (cuttedStr.length() < 3) { cuttedStr = "0" + cuttedStr; }
				 * 加上dot，以显示小数点后两位 cuttedStr = cuttedStr.substring(0,
				 * cuttedStr.length() - 2) + "." +
				 * cuttedStr.substring(cuttedStr.length() - 2);
				 * 
				 * mBuyPriceEdit.setText(cuttedStr);
				 * 
				 * mBuyPriceEdit.setSelection(mBuyPriceEdit.length()); isChanged
				 * = false;
				 */
            }
        });
    }

    @Override
    public void onClick(View v) {
        DeviceHelper.hideIME(mTopbarView);
        switch (v.getId()) {
            case R.id.layout_product:
                selectProduct();
                break;
            case R.id.layout_buy_unit:
                selectUnit();
                break;
            case R.id.layout_start_time:
                selectStartDate();
                break;
            case R.id.layout_end_time:
                selectEndDate();
                break;
            case R.id.layout_recev_address:
                selectAddressPoi();
                break;
            case R.id.imageview_photo1:
                selectImage(UPLOAD_PHOTO1);
                break;
            case R.id.imageview_photo2:
                selectImage(UPLOAD_PHOTO2);
                break;
            case R.id.imageview_photo3:
                selectImage(UPLOAD_PHOTO3);
                break;
            case R.id.imageview_photo4:
                selectImage(UPLOAD_PHOTO4);
                break;
            case R.id.imageview_photo5:
                selectImage(UPLOAD_PHOTO5);
                break;
            case R.id.imageview_photo6:
                selectImage(UPLOAD_PHOTO6);
                break;
            case R.id.button_next:
                showSureDialog();
                break;
            case R.id.iv_open:
                if (isOpen)
                    close();
                else
                    open();
                break;
            case R.id.layout_price_type:

                break;
            case R.id.layout_specifications:
                //选择产品
                if (selectVariety == null) {
                    showToast("请先选择品种");
                    return;
                }else{
                    selectSpecifications();
                }
                break;
        }
    }

    private void showSureDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认发布?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (isEditStatus) {
                    uploadImageEdit();
                } else {
                    uploadImage();
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.create().show();
    }

    private void uploadImageEdit() {
        EditSupplyPublished supply = new EditSupplyPublished();
        supply.setNumber(currentSupplyOrder.getNumber());
        if (ecCategorySecond == null) {
            showToast("请选择产品类型");
            return;
        }
//        String standard = mStandardEdit.getText().toString().trim();
        String specifications = "";

        if(!selectSpecification.isEmpty()){
            Collection<List<Specification>> values = selectSpecification.values();
            for (List<Specification> list : values) {
                for (Specification specification : list) {
                    specifications += specification.id + ",";
                }
            }
            specifications = specifications.substring(0, specifications.length() - 1);
        }


        if (startPoi == null) {
            showToast("请选择供货地址");
            return;
        }

        String address = mDetailAddressText.getText().toString().trim();
        if (TextUtils.isEmpty(address)) {
            showToast("请输入详细地址");
            return;
        }

        // String realname = mHaimai.getText().toString().trim();
        // String mobile = mGongsi.getText().toString().trim();
        // if (TextUtils.isEmpty(realname)) {
        // showToast("请输入联系人姓名");
        // return;
        // }
        // if (TextUtils.isEmpty(mobile)) {
        // showToast("请输入联系人电话");
        // return;
        // }
        // if (!StringHelper.isPhoneNumber(mobile)) {
        // showToast("请输入合格的联系人电话");
        // return;
        // }


        String money = mBuyPriceEdit.getText().toString().trim();

        if (TextUtils.isEmpty(money)) {
            showToast("请输入供货价格");
            return;
        }

        String minsell = mQidingxianzhiEdit.getText().toString().trim();
        if (TextUtils.isEmpty(minsell)) {
            showToast("请输入起售限制");
            return;
        }
        // 发布到商机
        if (mReleaseTo.isChecked()) {
            String haiMai = mHaimai.getText().toString().trim();
            String gongSi = mGongsi.getText().toString().trim();
            if (!TextUtils.isEmpty(haiMai)) {
                supply.setSearchtxt(haiMai);
            }
            if (!TextUtils.isEmpty(gongSi)) {
                supply.setHistroySupply(gongSi);
            }

        }

        String cityid = startPoi.getId();
        String lng = startPoi.getLng() + "";
        String lat = startPoi.getLat() + "";

        unit = mBuyUnitText.getText().toString();

        String varieties = "";
        if (selectVariety != null) {
            varieties = selectVariety.getCatid();
        }

        // String varieties = "";
        // StringBuilder sb = new StringBuilder();
        // for(EcCategoryThird item:mSelectVarietyList){
        // sb.append(item.getPid()+",");
        // }
        // varieties = sb.toString();

        String content = edittext_mark.getText().toString().trim();

        supply.setContent(content);


        supply.setTitle(ecCategorySecond.getCatname());
        supply.setBigpid(ecCategorySecond.getBigpid());
        supply.setPid(ecCategorySecond.getCatid());
        supply.setCatid(varieties);
        // supply.setRealname(realname);
        // supply.setMobile(mobile);
        supply.setImages(images);
        supply.setMoney(money);
        supply.setMoneyType(priceType);
        supply.setUnit(unit);
        supply.setCityid(cityid);
        supply.setLng(lng);
        supply.setLat(lat);
        supply.setAddress(address);
        supply.setMinsell(minsell);
        supply.setSpecifications(specifications);
        supply.setStart(sendDate);
        supply.setEnd(wantDate);
        supply.setShuai(mShuaihuoCb.isChecked() ? "1" : "2");

        if (TextUtils.isEmpty(mPhoto1Uri) && TextUtils.isEmpty(mPhoto2Uri) && TextUtils.isEmpty(mPhoto3Uri)
                && TextUtils.isEmpty(mPhoto4Uri) && TextUtils.isEmpty(mPhoto5Uri) && TextUtils.isEmpty(mPhoto6Uri)) {
            releaseEdit(supply);
        } else {
            uploadFile(supply, mPhoto1Uri, FileType.IMAGE, UPLOAD_PHOTO1);
        }
    }

    private void releaseEdit(EditSupplyPublished supply) {
        startLoadingDialog();
        RequestService.getInstance().publishedSupplyRecordEdit(this, supply, BaseEntity.class, new RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    showToast("修改成功");
                    // finish();
                    toSupplyHall();
                    finish();
                } else {
                    showToast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    /**
     * 上传图片
     */
    private void uploadImage() {
        SupplyPublished supply = new SupplyPublished();

        if (ecCategorySecond == null) {
            showToast("请选择产品类型");
            return;
        }
        String specifications = "";
        if(!selectSpecification.isEmpty()){
            Collection<List<Specification>> values = selectSpecification.values();
            for (List<Specification> list : values) {
                for (Specification specification : list) {
                    specifications += specification.id + ",";
                }
            }
            specifications = specifications.substring(0, specifications.length() - 1);
        }
        if (startPoi == null) {
            showToast("请选择供货地址");
            return;
        }

        String address = mDetailAddressText.getText().toString().trim();
        if (TextUtils.isEmpty(address)) {
            showToast("请输入详细地址");
            return;
        }

        // String realname = mHaimai.getText().toString().trim();
        // String mobile = mGongsi.getText().toString().trim();
        // if (TextUtils.isEmpty(realname)) {
        // showToast("请输入联系人姓名");
        // return;
        // }
        // if (TextUtils.isEmpty(mobile)) {
        // showToast("请输入联系人电话");
        // return;
        // }
        // if (!StringHelper.isPhoneNumber(mobile)) {
        // showToast("请输入合格的联系人电话");
        // return;
        // }

        String money = mBuyPriceEdit.getText().toString().trim();

        if (TextUtils.isEmpty(money)) {
            showToast("请输入供货价格");
            return;
        }

        String minsell = mQidingxianzhiEdit.getText().toString().trim();
        if (TextUtils.isEmpty(minsell)) {
            showToast("请输入起售限制");
            return;
        }
        // 发布到商机
        if (mReleaseTo.isChecked()) {
            String haiMai = mHaimai.getText().toString().trim();
            String gongSi = mGongsi.getText().toString().trim();
            if (!TextUtils.isEmpty(haiMai)) {
                supply.setSearchtxt(haiMai);
            }
            if (!TextUtils.isEmpty(gongSi)) {
                supply.setHistroySupply(gongSi);
            }

        }

        String cityid = startPoi.getId();
        String lng = startPoi.getLng() + "";
        String lat = startPoi.getLat() + "";

        unit = mBuyUnitText.getText().toString();

        String varieties = "";
        if (selectVariety != null) {
            varieties = selectVariety.getCatid();
        }

        // String varieties = "";
        // StringBuilder sb = new StringBuilder();
        // for(EcCategoryThird item:mSelectVarietyList){
        // sb.append(item.getPid()+",");
        // }
        // varieties = sb.toString();

        String content = edittext_mark.getText().toString().trim();

        supply.setContent(content);


        supply.setTitle(ecCategorySecond.getCatname());
        supply.setBigpid(ecCategorySecond.getBigpid());
        supply.setPid(ecCategorySecond.getCatid());
        supply.setCatid(varieties);
        // supply.setRealname(realname);
        // supply.setMobile(mobile);
        supply.setImages(images);
        supply.setMoney(money);
        supply.setMoneyType(priceType);
        supply.setUnit(unit);
        supply.setCityid(cityid);
        supply.setLng(lng);
        supply.setLat(lat);
        supply.setAddress(address);
        supply.setMinsell(minsell);
        supply.setSpecifications(specifications);
        supply.setStart(sendDate);
        supply.setEnd(wantDate);
        supply.setShuai(mShuaihuoCb.isChecked() ? "1" : "2");

        if (TextUtils.isEmpty(mPhoto1Uri) && TextUtils.isEmpty(mPhoto2Uri) && TextUtils.isEmpty(mPhoto3Uri)
                && TextUtils.isEmpty(mPhoto4Uri) && TextUtils.isEmpty(mPhoto5Uri) && TextUtils.isEmpty(mPhoto6Uri)) {
            release(supply);
        } else {
            uploadFile(supply, mPhoto1Uri, FileType.IMAGE, UPLOAD_PHOTO1);
        }
    }

    /**
     * 发布
     *
     * @param supply
     */
    private void release(SupplyPublished supply) {
        startLoadingDialog();
        RequestService.getInstance().supplyPublish(this, supply, BaseEntity.class, new RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    showToast("发布成功");
                    // finish();
                    toSupplyHall();
                    finish();
                } else {
                    showToast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    protected void toSupplyHall() {
        Intent intent = new Intent(this, SupplyHallActivity.class);
        startActivity(intent);
    }

    /**
     * 上传图片
     *
     * @param supply
     * @param uri
     * @param image
     * @param uploadId
     */
    private void uploadFile(final SupplyPublished supply, String uri, String image, final int uploadId) {
        if (TextUtils.isEmpty(uri)) {
            UploadImageEntity entity = null;
            setUrl(uploadId, entity, supply);
            return;
        } else if (uri.startsWith("http")) {
            setUrl(uploadId, uri, supply);
            return;
        }


        startLoadingDialog();
        String isSupply = "supply";//添加水印的标志
        RequestService.getInstance().uploadImage(this, uri, image, isSupply, UploadImageEntity.class, new RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    UploadImageEntity entity = (UploadImageEntity) resultData;
                    setUrl(uploadId, entity, supply);
                } else {
                    showToast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(msg);
            }
        });
    }

    /**
     * @param uploadId
     * @param entity
     * @param releaseInfo
     */
    protected void setUrl(int uploadId, UploadImageEntity entity, SupplyPublished releaseInfo) {
        switch (uploadId) {
            case UPLOAD_PHOTO1:
                if (entity != null) {
                   String shippimgs =  entity.getData().getFile();
                    releaseInfo.setImages( (shippimgs == null ? "": shippimgs) + ",");
                }
                uploadFile(releaseInfo, mPhoto2Uri, FileType.IMAGE, UPLOAD_PHOTO2);
                break;
            case UPLOAD_PHOTO2:
                if (entity != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages((shippimgs == null ? "": shippimgs) + entity.getData().getFile() + ",");
                }
                uploadFile(releaseInfo, mPhoto3Uri, FileType.IMAGE, UPLOAD_PHOTO3);
                break;
            case UPLOAD_PHOTO3:
                if (entity != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages((shippimgs == null ? "": shippimgs) + entity.getData().getFile() + ",");
                }
                uploadFile(releaseInfo, mPhoto4Uri, FileType.IMAGE, UPLOAD_PHOTO4);
                break;
            case UPLOAD_PHOTO4:
                if (entity != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages((shippimgs == null ? "": shippimgs) + entity.getData().getFile() + ",");
                }
                uploadFile(releaseInfo, mPhoto5Uri, FileType.IMAGE, UPLOAD_PHOTO5);
                break;
            case UPLOAD_PHOTO5:
                if (entity != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages((shippimgs == null ? "": shippimgs) + entity.getData().getFile() + ",");
                }
                uploadFile(releaseInfo, mPhoto6Uri, FileType.IMAGE, UPLOAD_PHOTO6);
                break;
            case UPLOAD_PHOTO6:
                if (entity != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages((shippimgs == null ? "": shippimgs) + entity.getData().getFile() + ",");
                }
                if (isEditStatus) {
                    releaseEdit((EditSupplyPublished) releaseInfo);
                } else {
                    release(releaseInfo);
                }
                break;
        }
    }

    /**
     * @param uploadId
     * @param url
     * @param releaseInfo
     */
    protected void setUrl(int uploadId, String url, SupplyPublished releaseInfo) {
        switch (uploadId) {
            case UPLOAD_PHOTO1:
                if (url != null) {
                    releaseInfo.setImages(url + ",");
                }
                uploadFile(releaseInfo, mPhoto2Uri, FileType.IMAGE, UPLOAD_PHOTO2);
                break;
            case UPLOAD_PHOTO2:
                if (url != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages(shippimgs + url + ",");
                }
                uploadFile(releaseInfo, mPhoto3Uri, FileType.IMAGE, UPLOAD_PHOTO3);
                break;
            case UPLOAD_PHOTO3:
                if (url != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages(shippimgs + url + ",");
                }
                uploadFile(releaseInfo, mPhoto4Uri, FileType.IMAGE, UPLOAD_PHOTO4);
                break;
            case UPLOAD_PHOTO4:
                if (url != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages(shippimgs + url + ",");
                }
                uploadFile(releaseInfo, mPhoto5Uri, FileType.IMAGE, UPLOAD_PHOTO5);
                break;
            case UPLOAD_PHOTO5:
                if (url != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages(shippimgs + url + ",");
                }
                uploadFile(releaseInfo, mPhoto6Uri, FileType.IMAGE, UPLOAD_PHOTO6);
                break;
            case UPLOAD_PHOTO6:
                if (url != null) {
                    String shippimgs = releaseInfo.getImages();
                    releaseInfo.setImages(shippimgs + url + ",");
                }
                if (isEditStatus) {
                    releaseEdit((EditSupplyPublished) releaseInfo);
                } else {
                    release(releaseInfo);
                }
                break;
        }
    }

    /**
     * 选择 图片
     *
     * @param selectType
     */
    private void selectImage(int selectType) {

        currentSelectImageType = selectType;

        AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
        builder1.setItems(new String[]{"相机", "相册"}, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        /*getCropParams().uri = CameraUtil.buildUri(System.currentTimeMillis() + ".jpg");
                        CropHelper.clearCachedCropFile(mCropParams.uri);
                        // CropHelper.sendCamreaSelectPhoto(SellsGoodsActivity.this);
                        Intent intent = CropHelper.buildCaptureIntent(getCropParams().uri);
                        startActivityForResult(intent, CropHelper.REQUEST_CAMERA);*/
                        CameraUtil.capture(SellsGoodsActivity.this);
                        break;
                    case 1:
                        CameraUtil.photo(SellsGoodsActivity.this);
                        /*mCropParams.uri = CameraUtil.buildUri(System.currentTimeMillis() + ".jpg");
                        CropHelper.clearCachedCropFile(mCropParams.uri);
                        // CropHelper.sendGalleryPhotoForResult(SellsGoodsActivity.this);
//                        CropHelper.clearCachedCropFile(mCropParams.uri);
                        startActivityForResult(CropHelper.buildCropFromGalleryIntent(mCropParams), CropHelper.REQUEST_CROP);*/
                        break;
                }
            }
        });
        builder1.create().show();
    }

    /**
     * 选择地址
     */
    private void selectAddressPoi() {
        // new CascadingMenuPopWindow().showInstance(this, mRecivAddressText,
        // new NMCascadingMenuViewOnSelectListener());
        if (cascadingMenuPopWindow == null) {
            // 向三级menu添加地区数据
            cascadingMenuPopWindow = new CascadingMenuPopWindow(this, false);
            cascadingMenuPopWindow.setMenuViewOnSelectListener(new NMCascadingMenuViewOnSelectListener());
            cascadingMenuPopWindow.showAsDropDown(mRecivAddressText, 5, 5);
        } else if (cascadingMenuPopWindow != null && cascadingMenuPopWindow.isShowing()) {
            cascadingMenuPopWindow.dismiss();
        } else {
            cascadingMenuPopWindow.showAsDropDown(mRecivAddressText, 5, 5);
        }
		/*
		 * Intent intent1 = new Intent(this, SelectCountrysActivity.class);
		 * startActivityForResult(intent1, SELECT_ADDRESS_POI);
		 */
    }

    // 级联菜单选择回调接口
    class NMCascadingMenuViewOnSelectListener implements CascadingMenuViewOnSelectListener {

        @Override
        public void getValue(Area city, String fullname) {
            mRecivAddressText.setText(fullname);
            startPoi = PoiEntityUtils.convert(city, fullname);
        }

    }

    /**
     * 选择开始时间
     */
    private void selectStartDate() {
        if (startDatePop != null && startDatePop.isShowing()) {
            return;
        }
        if (startDatePop == null) {

            startDatePop = new DatepickerStartPopwindow(this);
            startDatePop.setOnSelectDateListener(new OnSelectDateListener() {

                @Override
                public void onSelectDate(String date) {
                    sendDate = date;
                    mStartTimeText.setText(sendDate);
                }
            });
        }
        startDatePop.showLocation(mTopbarView);
    }

    /**
     * 选择结束时间
     */
    private void selectEndDate() {
        if (endDatePop != null && endDatePop.isShowing()) {
            return;
        }
        if (endDatePop == null) {

            endDatePop = new DatepickerPopwindow(this);
            endDatePop.setOnSelectDateListener(new OnSelectDateListener() {

                @Override
                public void onSelectDate(String date) {
                    wantDate = date;
                    mEndTimeText.setText(wantDate);
                }
            });
        }
        endDatePop.showLocation(mTopbarView);
    }

    /**
     * 选择种类
     */
    private void selectProduct() {
        Intent intent = new Intent(this, ProductTypeActivity.class);
        intent.putExtra(ExtraName.action, true);
        startActivityForResult(intent, SELECT_PRODUCT_TYPE);
    }

    /**
     * 选择品种
     */
    private void selectSpecifications() {
        Intent intent = new Intent(this, SelectSpecificationsActivity.class);
        intent.putExtra(ExtraName.ecCategoryThird,selectVariety);
        startActivityForResult(intent, SELECT_SPECIFICATIONS);
    }

    /**
     * 选择 数量单位
     */
    protected void selectUnit() {
        if (unitDialog != null && unitDialog.isShowing()) {
            return;
        }
        if (unitDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setItems(EcOption.UNIT, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    unit = EcOption.UNIT[which];
                    // setCarRequireText();
                    mBuyUnitText.setText(EcOption.UNIT[which]);
                    mLimitUnitText.setText(EcOption.UNIT[which]);
                }
            });
            unitDialog = builder.create();
            unitDialog.setTitle("单位");
        }
        unitDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        CropHelper.handleResult(this, requestCode, resultCode, data);
        CameraUtil.handleResult(this, requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case SELECT_ADDRESS_POI:
                    startPoi = (PoiEntity) data.getSerializableExtra(ExtraName.poi);
                    mRecivAddressText.setText(startPoi.getTitle());
                    break;
                case SELECT_PRODUCT_TYPE:
                    EcCategorySecond second = (EcCategorySecond) data.getSerializableExtra(ExtraName.ecCategorySecond);
                    if (ecCategorySecond != null && !TextUtils.isEmpty(ecCategorySecond.getCatid())
                            && ecCategorySecond.getCatid().equals(second.getCatid())) {
                    } else {

                        if (ecCategorySecond != null && second.getCatname().equals("全部")
                                && ecCategorySecond.getCatname().equals("全部")) {
                            return;
                        }
                        ecCategorySecond = second;
                        mProductEdit.setText(ecCategorySecond.getCatname());
                        mSelectVarietyList.clear();
                        getVariety(ecCategorySecond);
                    }
                    break;
//                case CropHelper.REQUEST_CAMERA:
//                    onPhotoCropped(getCropParams().uri);
//                    break;
//                case CropHelper.REQUEST_CROP:
//                    onPhotoCropped(Uri.parse(CropFileUtils.getSmartFilePath(this, data.getData())));
//                    break;
            }
        }else if(resultCode == SelectSpecificationsActivity.SELECT_OK){
                selectSpecification = (Map<Specification, List<Specification>>) data.getSerializableExtra(ExtraName.isForResult);
                if (!selectSpecification.isEmpty()) {

                    gv_specification.setAdapter(new SpecificationAdapter(this, gv_specification, selectSpecification));

                    fl_specification.setVisibility(View.VISIBLE);
                    layout_specifications.setVisibility(View.GONE);
                }else{
                    fl_specification.setVisibility(View.GONE);
                    layout_specifications.setVisibility(View.VISIBLE);
                }
        }
    }

    /**
     * 根据 名称 请求种类
     *
     * @param item
     */
    private void getVariety(EcCategorySecond item) {
        startLoadingDialog();
        RequestService.getInstance().getEcCategoryVariety(this, item.getBigpid(), item.getCatid(),
                EcVarietyEntity.class, new RequestListener() {

                    @Override
                    public void onSuccess(int requestCode, BaseEntity resultData) {
                        dismissLoadingDialog();
                        if (resultData.isOk()) {
                            EcVarietyEntity entity = (EcVarietyEntity) resultData;
                            setVarietyAdapter(entity.getData().getList());
                        } else {
                            showToast(resultData.getMsg());
                        }
                    }

                    @Override
                    public void onFailed(int requestCode, Exception error, String msg) {
                        dismissLoadingDialog();
                        showToast(R.string.please_check_network);
                    }
                });
    }

    /**
     * 填充 数据
     *
     * @param list
     */
    protected void setVarietyAdapter(List<EcCategoryThird> list) {
        if (mVarietyAdapter == null) {
            mVarietyAdapter = new VarietyGridviewAdapter<>(this, list, false);
            mVarietyAdapter.setSingleSeletion(true);
            mPinzhongGv.setAdapter(mVarietyAdapter);
        } else {
            mVarietyAdapter.setData(list);
        }
        ObjectAnimator.ofFloat(mOpenView, "rotation", 0, 180).setDuration(250).start();
//		isOpen = true;// 因为 动态获取 gridView 的高度 不准确 所以 现在让它自己 通过adapter 填充 来展开
        // 当关闭时 已经填充满了 所以可以拿到高度
        open();
        mPinZhongView.setText("");
        mPinZhongView.setVisibility(View.GONE);
    }

    @Override
    public void onPhotoCropped(Uri u) {
        String uri = u.getPath();
        Logger.d(Logger.LogTag.UI, "Crop Uri in path: " + uri);
        // Toast.makeText(this, "Photo cropped!", Toast.LENGTH_LONG).show();

        switch (currentSelectImageType) {
            case UPLOAD_PHOTO1:
                ImageLoader.getInstance().displayImage("file://" + uri, mPhoto1Image,
                        ImageLoadUtil.getDisplayImageOptions(0, 10));
                // LoadUtils.getInstance().loadImage(mPhoto1Image, "file://" + uri,
                // 0, 5);
                mPhoto1Uri = uri;
                break;
            case UPLOAD_PHOTO2:
                ImageLoader.getInstance().displayImage("file://" + uri, mPhoto2Image,
                        ImageLoadUtil.getDisplayImageOptions(0, 10));
                // LoadUtils.getInstance().loadImage(mPhoto2Image, "file://" + uri,
                // 0, 5);
                mPhoto2Uri = uri;
                break;
            case UPLOAD_PHOTO3:
                ImageLoader.getInstance().displayImage("file://" + uri, mPhoto3Image,
                        ImageLoadUtil.getDisplayImageOptions(0, 10));
                // LoadUtils.getInstance().loadImage(mPhoto3Image, "file://" + uri,
                // 0, 5);
                mPhoto3Uri = uri;
                break;
            case UPLOAD_PHOTO4:
                ImageLoader.getInstance().displayImage("file://" + uri, mPhoto4Image,
                        ImageLoadUtil.getDisplayImageOptions(0, 10));
                // LoadUtils.getInstance().loadImage(mPhoto4Image, "file://" + uri,
                // 0, 5);
                mPhoto4Uri = uri;
                break;
            case UPLOAD_PHOTO5:
                ImageLoader.getInstance().displayImage("file://" + uri, mPhoto5Image,
                        ImageLoadUtil.getDisplayImageOptions(0, 10));
                // LoadUtils.getInstance().loadImage(mPhoto5Image, "file://" + uri,
                // 0, 5);
                mPhoto5Uri = uri;
                break;
            case UPLOAD_PHOTO6:
                ImageLoader.getInstance().displayImage("file://" + uri, mPhoto6Image,
                        ImageLoadUtil.getDisplayImageOptions(0, 10));
                // LoadUtils.getInstance().loadImage(mPhoto6Image, "file://" + uri,
                // 0, 5);
                mPhoto6Uri = uri;
                break;
        }

        // mFrontIdImage.setImageBitmap(CropHelper.decodeUriAsBitmap(this,
        // mCropParams.uri));
        // uploadInfo();
    }

    @Override
    public void onCropCancel() {
        // Toast.makeText(this, "Crop canceled!", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCropFailed(String message) {
        Toast.makeText(this, "Crop failed:" + message, Toast.LENGTH_LONG).show();
    }

    @Override
    public CropParams getCropParams() {
        return mCropParams;
    }

    @Override
    public Activity getContext() {
        return this;
    }

    /**
     * 选择产品 种类 时 自动隐藏
     */
    private OnItemClickListener mVarietyItemListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectVariety = (EcCategoryThird) parent.getItemAtPosition(position);
            mVarietyAdapter.setSelectPosition(position);
            mPinZhongView.setText(selectVariety.getCatname());
            mPinZhongView.setVisibility(View.VISIBLE);
            close();
        }
    };
    private EditText edittext_mark;

    @Override
    protected void onDestroy() {
        if (cascadingMenuPopWindow != null) {
            cascadingMenuPopWindow.dismiss();
        }
        if(this.getCropParams() != null) {
            CropHelper.clearCachedCropFile(this.getCropParams().uri);
        }
        super.onDestroy();
    }

    /**
     * 隐藏底部的发布模块
     */
    private void hideRelease() {
        if (mReleaseView == null) {
            mReleaseView = (LinearLayout) findViewById(R.id.ll_release);
            mReleaseView.measure(0, 0);
            releaseHight = mReleaseView.getMeasuredHeight();
        }
        ValueAnimator animator = ValueAnimator.ofInt(releaseHight, 0);
        animator.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mReleaseView.getLayoutParams().height = (int) animation.getAnimatedValue();
                mReleaseView.requestLayout();

            }
        });
        animator.setDuration(250);
        animator.start();
    }

    /**
     * 显示底部的 发布模块
     */
    private void showRelease() {
        ValueAnimator animator = ValueAnimator.ofInt(0, releaseHight);
        animator.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mReleaseView.getLayoutParams().height = (int) animation.getAnimatedValue();
                mReleaseView.requestLayout();
                scrollView.smoothScrollBy(0, releaseHight - (int) animation.getAnimatedValue());
            }
        });
        animator.setDuration(250);
        animator.start();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked)
            showRelease();
        else
            hideRelease();
    }

    /**
     * 打开 gridView
     */
    public void open() {
        height = measureHeight(mPinzhongGv);
        if (isAnim || height == 0)
            return;
        final ValueAnimator animator = ValueAnimator.ofInt(-height, 0);
        animator.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mPinzhongGv.setPadding(0, (int) animator.getAnimatedValue(), 0, 0);
                mPinzhongGv.requestLayout();
            }
        });
        animator.setDuration(250);
        animator.addListener(new AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                ObjectAnimator.ofFloat(mOpenView, "rotation", 0, 180).setDuration(250).start();
                isAnim = true;
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                isAnim = false;
                isOpen = true;

            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }
        });
        mPinzhongGv.setVisibility(View.VISIBLE);
        animator.start();
    }

    /**
     * 关闭gridView
     */
    public void close() {
//		height = mPinzhongGv.getHeight();
//		height = mPinzhongGv.getLayoutParams().height;
        height = measureHeight(mPinzhongGv);
        if (isAnim || height == 0)
            return;
        final ValueAnimator animator = ValueAnimator.ofInt(0, -height);
        animator.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mPinzhongGv.setPadding(0, (int) animator.getAnimatedValue(), 0, 0);
                mPinzhongGv.requestLayout();
            }
        });
        animator.setDuration(250);
        animator.addListener(new AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                ObjectAnimator.ofFloat(mOpenView, "rotation", 180, 360).setDuration(250).start();
                isAnim = true;
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                isAnim = false;
                isOpen = false;
                mPinzhongGv.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }
        });
        animator.start();
    }

    public int measureHeight(GridView gridView) {
        int count = gridView.getChildCount();
        int rowCount = count / 3 + (count % 3 == 0 ? 0 : 1);
        View childView = gridView.getAdapter().getView(0, null, null);
//		View childView =  gridView.getChildAt(0);
        childView.measure(0, 0);
        int height = childView.getMeasuredHeight();
        int verticalSpacing = ViewUtil.dip2px(this, 5f);
        return height * rowCount + (rowCount - 1) * verticalSpacing;
    }
}
